package com.cq.hd.merchant.controller;

import com.cq.hd.common.entity.MerchantAppLoginUser;
import com.cq.hd.common.enums.biz.PlatformTypeEnum;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.PopRecordApi;
import com.cq.hd.member.api.TbMerchantApi;
import com.cq.hd.member.api.dto.PopRecordSaveDto;
import com.cq.hd.member.api.vo.MerchantVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/popRecord")
@Api(tags = "弹窗相关接口")
public class PopRecordController extends BaseController {

    @Autowired
    private PopRecordApi popRecordApi;

    @Autowired
    private TbMerchantApi merchantApi;

    @ApiOperation(value = "弹窗")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "popType", value = "弹窗类型(1-商家入驻/2-商户入驻)", paramType = "path", dataType = "Integer"),
            @ApiImplicitParam(name = "openId", value = "openId(token和openId，二选一)", paramType = "query", dataType = "String")
    })
    @GetMapping("/save/{popType}")
    public Result<Boolean> save(@PathVariable("popType") Integer popType, String openId) {
        Long merchantId;
        MerchantAppLoginUser merchantAppLoginUser = getMerchantAppLoginUser();
        if (merchantAppLoginUser == null) {
            if (StringUtils.isBlank(openId)) {
                Throw.isBusinessException("请授权");
            }

            MerchantVo merchantVo = merchantApi.getByOpenId(openId).unpack();
            if (merchantVo == null) {
                Throw.isBusinessException("未找到商户数据");
            }
            merchantId = merchantVo.getId();
        } else {
            merchantId = merchantAppLoginUser.getMerchantId();
        }

        PopRecordSaveDto popRecordSaveDto = new PopRecordSaveDto();
        popRecordSaveDto.setPlatformType(PlatformTypeEnum.MERCHANT.getValue());
        popRecordSaveDto.setPopType(popType);
        popRecordSaveDto.setUserId(merchantId);
        popRecordSaveDto.setCreateTime(LocalDateTime.now());
        return popRecordApi.savePopRecord(popRecordSaveDto);
    }


//    @ApiOperation(value = "根据openId弹窗")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "popType", value = "弹窗类型(1-商家入驻/2-商户入驻)", paramType = "path", dataType = "Integer"),
//            @ApiImplicitParam(name = "openId", value = "openId", paramType = "query", dataType = "String")
//    })
//    @GetMapping("/saveByOpenId/{popType}")
//    public Result<Boolean> saveByOpenId(@PathVariable("popType") Integer popType, String openId) {
//        if (StringUtils.isBlank(openId)) {
//            Throw.isBusinessException("请授权");
//        }
//
//        MerchantVo merchantVo = merchantApi.getByOpenId(openId).unpack();
//        if (merchantVo == null) {
//            Throw.isBusinessException("未找到商户数据");
//        }
//
//        PopRecordSaveDto popRecordSaveDto = new PopRecordSaveDto();
//        popRecordSaveDto.setPlatformType(PlatformTypeEnum.MERCHANT.getValue());
//        popRecordSaveDto.setPopType(popType);
//        popRecordSaveDto.setUserId(merchantVo.getId());
//        popRecordSaveDto.setCreateTime(LocalDateTime.now());
//        return popRecordApi.savePopRecord(popRecordSaveDto);
//    }


}
